const { checkout } = require('../routes');
const dbConfig = require('../utils/dbconfig');
var querystring = require('querystring');
const common = require('../utils/common')

// 评论加一
addRemark = (qid)=>{
  let sql = 'update question set remarknum=remarknum+1 where qid=?';
  let sqlArr = [qid];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        return 200
      }else{
        return 400
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 人气加一
addDiscuss= (qid)=>{
  let sql = 'update question set discussNum=discussNum+1 where qid=?';
  let sqlArr = [qid];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        return 200
      }else{
        return 400
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 收藏加一
addStar = (qid)=>{
  let sql = 'update question set starnum=starnum+1 where qid=?';
  let sqlArr = [qid];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        return 200
      }else{
        return 400
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 收藏减一
reduceStar=(qid)=>{
  let sql = 'update question set starnum=starnum-1 where qid=?';
  let sqlArr = [qid];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        return 200
      }else{
        return 400
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到所有杠题
getSubjectInfo = (req,res)=>{
  let sql = 'SELECT * from user inner join question using(uid) ORDER BY posttime DESC limit ?,?';
  let sqlArr = [parseInt(req.query.start) ,parseInt(req.query.num)];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到所有杠题不分页
getAllSubjectInfo = (req,res)=>{
  let sql = 'SELECT * from user inner join question using(uid) ORDER BY posttime DESC';
  let sqlArr = [];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到官方杠题
getOfficialSubject = (req,res)=>{
  let sql = 'SELECT * FROM question WHERE uid=1 AND officialFlag=1';
  let sqlArr = [];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到杠题弹幕
getBullet= (req,res)=>{
  let sql = 'SELECT user.userImg,bulletchat.bcontent from user inner join bulletchat using(uid) WHERE bulletchat.qid=?';
  let sqlArr = [ req.query.qid ];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 发射弹幕
insertBullet=(req,res)=>{
  let params = req.body;
  let sql = 'insert into bulletchat(uid,qid,bcontent) value(?,?,?)';
  let sqlArr = [params.uid,params.qid,params.bcontent];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        addDiscuss(params.qid)
        res.send({
          'code':200,
          'message':'发射成功'
        });
      }else{
        res.send({
          'code':400,
          'message':'发射失败'
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到指定杠题的评论
getRemarkByQid=(req,res)=>{
  let sql = 'SELECT * from user inner join remark using(uid) WHERE qid=? ORDER BY rDate DESC';
  let sqlArr = [req.query.qid];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 发表评论
sendComment=(req,res)=>{
  let params = req.body;
  let sql = 'insert into remark(qid,uid,rcontent,rDate,`option`) value(?,?,?,?,?)';
  let sqlArr = [params.qid,params.uid,params.rcontent,params.rDate,params.option];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        addRemark(params.qid)
        res.send({
          'code':200,
          'message':'发表成功'
        });
      }else{
        res.send({
          'code':400,
          'message':'发表失败'
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到指定用户选择表
getOptionsByUid=(req,res)=>{
  let sql = 'SELECT * FROM `options` WHERE uid=?';
  let sqlArr = [ req.query.uid ];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到所有题目选择表
getOptionsByQid=(req,res)=>{
  let sql = 'SELECT * FROM `options`';
  let sqlArr = [];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 选择选项
chooseOption=(req,res)=>{
  let params = req.body;
  let sql = 'insert into `options`(qid,uid,useroption,optionDate) value(?,?,?,?)';
  let sqlArr = [params.qid,params.uid,params.useroption,params.optionDate];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        addDiscuss(params.qid)
        res.send({
          'code':200,
          'message':'发表成功'
        });
      }else{
        res.send({
          'code':400,
          'message':'发表失败'
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 新建杠题
insertSubject=(req,res)=>{
  let typeId = common.rand(1,6)
  let params = req.body;
  let sql='insert into question(qtitle,qdesc,examine,optionOne,optionTwo,remarknum,starnum,posttime,uid,discussNum,officialFlag,tid) value(?,?,?,?,?,?,?,?,?,?,?,?)';
  let sqlArr=[params.qtitle,params.qdesc,1,params.optionOne,params.optionTwo,0,0,params.posttime,params.uid,0,0,typeId];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        res.send({
          'code':200,
          'message':'发表成功'
        });
      }else{
        res.send({
          'code':400,
          'message':'发表失败'
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)

}
// 得到收藏表
getStar=(req,res)=>{
  let sql = 'SELECT * FROM `star`';
  let sqlArr = [];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}
// 得到指定用户收藏表
getStarByUid=(req,res)=>{
  let sql = 'SELECT * FROM `star` WHERE uid=?';
  let sqlArr = [ req.query.uid ];
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}
// 点击收藏
addStarByUser=(req,res)=>{
  let params = req.body;
  let sql = 'insert into `star`(uid,qid,sDate) value(?,?,?)';
  let sqlArr = [params.uid,params.qid,params.sDate];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        addStar(params.qid)
        res.send({
          'code':200,
          'message':'收藏成功'
        });
      }else{
        res.send({
          'code':400,
          'message':'收藏失败'
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)

  addStar()
}

// 取消收藏
reduceStarByUser=(req,res)=>{
  let params = req.body;
  let sql = 'DELETE FROM star WHERE uid=? AND qid=?';
  let sqlArr = [params.uid,params.qid];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.affectedRows === 1){
        reduceStar(params.qid)
        res.send({
          'code':200,
          'message':'取消成功'
        });
      }else{
        res.send({
          'code':400,
          'message':'取消失败'
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)

  addStar()
}

// 模糊搜索
vagueSearch=(req,res)=>{
  let sql = `select * from question inner join user using(uid) where qtitle like '%${req.query.content}%' ORDER BY addDate DESC`;
  let sqlArr = [];
  let callBack = (err,data)=>{
    if(err){
      console.log(err);
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到指定用户杠题表
getSubjectByUid=(req,res)=>{
  let sql='SELECT * from user inner join question using(uid) WHERE uid=? ORDER BY posttime DESC'
  let sqlArr=[req.query.uid]
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到指定用户收藏的杠题表
getSubjectByStar=(req,res)=>{
  let sql='SELECT * from (SELECT * from star inner join user using(uid) WHERE uid=?) as star INNER JOIN question using(qid) ORDER BY posttime DESC'
  let sqlArr=[req.query.uid]
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}

// 得到指定用户选择过的杠题表
getSubjectByOption=(req,res)=>{
  let sql='SELECT * from (SELECT * from options inner join user using(uid) WHERE uid=1) as star INNER JOIN question using(qid) ORDER BY posttime DESC'
  let sqlArr=[req.query.uid]
  let callBack = (err,data)=>{
    if(err){
      console.log('连接出错');
    }else{
      if(data.length > 0){
        res.send({
          'code':200,
          'data':data
        });
      }else{
        res.send({
          'code':200,
          'data':[]
        });
      }
    }
  };
  dbConfig.sqlConnect(sql,sqlArr,callBack)
}
module.exports = {
  getSubjectInfo,
  getOfficialSubject,
  getBullet,
  insertBullet,
  getRemarkByQid,
  sendComment,
  getOptionsByUid,
  getOptionsByQid,
  chooseOption,
  insertSubject,
  getStar,
  addStarByUser,
  reduceStarByUser,
  getStarByUid,
  vagueSearch,
  getAllSubjectInfo,
  getSubjectByUid,
  getSubjectByStar,
  getSubjectByOption
}